How to get MAC address from c# [migrated]

Posted by Andrew Simpson on Programmers See other posts from Programmers or by Andrew Simpson
Published on 2013-10-26T11:53:10Z Indexed on 2013/10/26 16:07 UTC
Read the original article Hit count: 289

Filed under:

I have a C# application. In a routine I have code to get the MAC address from using SendARP.

It works on Windows 7 but does not work on Windows XP. I just get a null string returned.

This is my code.

Thanks...

System.Runtime.InteropServices.DllImport("iphlpapi.dll", ExactSpelling = true)]
        static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref int PhyAddrLen);
        public static PhysicalAddress GetMacAddress(IPAddress ipAddress)
        {
            const int MacAddressLength = 6; //i know it is has a length of 6
            int length = MacAddressLength;
            var macBytes = new byte[MacAddressLength];
            SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macBytes, ref length);
            return new PhysicalAddress(macBytes);
        }

© Programmers or respective owner

Related posts about c#